<template>
  <div class="header_flex">
    <div class="flex_l">
      <el-form label-width="120px" :rules="rules" class="user-search" label-position="right" :size="size" ref="form"
        :model="form" :inline="true">
        <div class="title">轮播图</div>
        <el-row>
          <el-col :span="25">
            <el-form-item label="轮播图：" prop="main_image" :rules="rules.main_image">
              <div class="text9">请上传3张不同图片，单张10MB以内，比例1:1最佳，以保证审核通过，拖拽图片可进行排序</div>
            </el-form-item>
          </el-col>
          <el-col :span="25">
            <el-form-item>
              <uploadImg numshow="9" uptext="添加图片" style="margin-left: 120px;" @changeName="setimgs" :up-height="120"
                :upWith="120" :imglist="form.main_image"></uploadImg>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="dialog-footer">
        <el-button :size="size" :loading="loading" type="primary" @click="submit('form')">
          <span v-if="!loading">提交</span>
          <span v-else>提 交 中...</span>
        </el-button>
      </div>

    </div>
  </div>
</template>

<script>
import uploadImg from '@/components/uploadImg .vue';
import { creatCarouse,carouseList } from '@/api/menu'
export default {
  components: {
    uploadImg
  },
  data() {
    var main_imagelength = (rule, value, callback) => {
      if (this.form.main_image.length < 3) {
        callback(new Error('最少三张图'))
      } else {
        callback()
      }
    }
    return {
      size: "mini ",
      form: {
        main_image: []
      },
      params: {
        title: '',
        page: 1
      },
      rules: {
        main_image: [
          { validator: main_imagelength, trigger: 'blur' }
        ]
      },
      loading: false,
    }
  },
  created() {
    carouseList().then(res => {
      res.response.forEach(element => {
        this.form.main_image.push(element.carouseUrl)
      });
      console.log(res, '轮播图');
    })
  },
  methods: {
    //单机提交做验证
    submit(form) {
      console.log(form)
      this.$refs[form].validate(valid => {
        if (valid) {
          console.log(this.form.main_image, 'form');
          creatCarouse(this.form.main_image).then(res => {
            this.$message.success('提交成功')
          })
        }
      })
    },
    //拿到字组件数组
    setimgs(e) {
      this.form.main_image = e
    },
  },
}
</script>

<style scoped>
.title {
  margin: 20px 0 20px 50px;
}

.text9 {
  color: #999;
  font-size: 14px;
}

.textareainput /deep/ .el-textarea__inner {
  height: 100px;
}

.flex_r::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
  color: transparent;
}

.header_flex {
  display: flex;
  background: #fff;
}

.dialog-footer {
  margin-left: 20%;
}
</style>